<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Web\Member\Buyer;

use App\Http\Controllers\Web\BaseController;
use App\Services\Cookie\Carts;
use Illuminate\Http\Request;

class CartsController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'buyer/carts'],
        'store' => ['post', 'buyer/carts'],
        'update' => ['put', 'buyer/carts/%d'],
        'delete' => ['delete', 'buyer/carts'],
        'get_carts' =>  ['post', 'buyer/carts/get_carts'],
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        if (session('user_id')) { //登录
            //请求部分
            $ret = http_web($this->api_url['index'])->send();

            //结果处理部分
            $data = [
                'valid_count' => $ret['data']['valid_list']['carts_count'],
                'valid_shops' => $ret['data']['valid_list']['shops'],
                'invalid_count' => $ret['data']['invalid_list']['carts_count'],
                'invalid_shops' => $ret['data']['invalid_list']['shops'],
            ];
            return view('web.order.cart', $data);
        }else{
            return redirect('/user/login');
        }
    }

    public function store(Carts $carts_service)
    {
        $data = $this->request->all();
        //@todo 往cookie中插入购物车信息
        $cookie_data=[];
        if($this->request->cookie('carts_data_arr')){
            $cookie_data=$this->request->cookie('carts_data_arr');
            $cookie_data=unserialize($cookie_data);
        }
        if (session('user_id')) { //已登录
            $user=session('user_id');
        }else{
            $user='';
        }
        $carts_service->create($data,$cookie_data,$user);

        if (session('user_id')) { //已登录
            $ret = http_web($this->api_url['store'])->send($data);

        }
        return response()->json(['code'=>0,'message'=>'加入购物车成功']);


    }

    public function edit()
    {

    }

    public function update($id)
    {

    }

    public function destroy(Carts $carts_service)
    {
        $this->validate($this->request, [
            'type' => 'required|in:all,batch',
            'cart_ids' => 'array',
            'sku_ids' => 'array'   //用于未登录用户的cookie删除
        ]);
        $data = $this->request->all(['type', 'cart_ids', 'sku_ids']);

        $ret = false;
        if($data['type'] == 'all'){
            //@todo 清空cookie购物车
            $ret = $carts_service->allDestroy();
        }elseif($data['type'] == 'batch'){
            //@todo 清除相关数据
            $ret = $carts_service->batchDestroy($data['cart_ids'], array_get($data, 'sku_ids'));
        }

        return response()->json(['code' => $ret ? 0 : 1, 'message' => $ret ? '删除商品成功' : '删除商品失败']);
    }

    /**
     * 获取cookie购物车
     * */
    public function get_carts(){

        if($this->request->cookie('carts_data_arr')){
            $cart=$this->request->cookie('carts_data_arr');
            $carts_data_arr=unserialize($cart);
            //查询cookie相关产品信息
            $ret['data']=$carts_data_arr;
        }else{
            $ret['data']=[];
        }
        return response()->json(['code'=>0,'message'=>'获取成功','data'=>session('user_id')]);
    }
}